﻿using MIO.Client;
using MIO.Client.Config;
using MIO.Client.Daemon;
using MIO.Client.Protocal;
using MIO.Client.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MIO.Client.Event;
using System.IO;
using System.Threading;

namespace MIOClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
             
            IOClientConfig conf = new IOClientConfig();
            conf.ServerIp = "127.0.0.1";
            conf.ServerPort = 15166;
            conf.AutoReConnect = true;
            conf.Debug = false;
            IOClient client = new IOClient(conf);

            client.Disconnect_Event += (sender, e) =>
            {
                Console.WriteLine("lost line");
            };

            //首次连接
            client.ConnectSuccess_Event += (sender, e) =>
            {
                Console.WriteLine("ConnectSuccess_Event");
                Sign sign = new Sign() { RoomId = 1, SeatId = 2, Token = "", Username = "213" };

                SignRes res = client.sendToServerReceive<SignRes>("sign", sign, "signRes");

                if (res?.Ok == true)
                {
                    Packet pp = new Packet()
                    {
                        Type = PacketType.EVENT_MESSAGE,
                        SubType = "join",
                        SessionId = "0",
                        DataContent = JsonUtil.object2JsonString(new JoinRoom()
                        {
                            ClientType = 1,
                            Id = 77,
                            Name = "张三",
                            RoomId = 1,
                            Token = "123456"
                        }),
                        Ack = true,
                        AckId = CommonUtil.createUniqueId()
                    };
                    if (client.send(pp))
                    {
                        Console.WriteLine("send " + pp.SubType + " success");
                    }
                    else
                    {
                        Console.WriteLine("send " + pp.SubType + " fail");
                    }
                }


            };

            client.addLostPacketDelegate(new LostPacketDelegate((c, packet) => {
                Console.WriteLine("lost packet");
            }));

            client.addEventDelegate<Chat>("chat", new EventPacketDelegate<Chat>((c, s, chat) => {
                Console.WriteLine(chat.Content);
            }));

            client.ReconnectSuccess_Event += (sender, e) =>
            {
                Packet pp = new Packet()
                {
                    Type = PacketType.EVENT_MESSAGE,
                    SubType = "join",
                    SessionId = "0",
                    DataContent = JsonUtil.object2JsonString(new JoinRoom()
                    {
                        ClientType = 1,
                        Id = 77,
                        Name = "张三",
                        RoomId = 1,
                        Token = "123456"
                    }),
                    Ack = true,
                    AckId = CommonUtil.createUniqueId()
                };
                if (client.send(pp))
                {
                    Console.WriteLine("send " + pp.SubType + " success");
                }
                else
                {
                    Console.WriteLine("send " + pp.SubType + " fail");
                }
            };

            client.EventManager.addEventDelegate("respGetOnlineClients", new EventPacketDelegate<ResponseGetOnlineClient>((c, fromSessionId, data) =>
            {
                Console.WriteLine(fromSessionId);
                ResponseGetOnlineClient res = data;
                foreach (var item in res.OnlineClientList)
                {
                    Console.WriteLine(item.SessionId + "-------" + item.JoinTime);
                }
            }));

            client.connect();


            while (true)
            {
                Console.WriteLine("id");
                string inputSessionId = Console.ReadLine();
                Console.WriteLine("c");
                string inputContent = Console.ReadLine();


                //Packet p = new Packet()
                //{
                //    Type = PacketType.EVENT_MESSAGE,
                //    SubType = "reqGetOnlineClients",
                //    SessionId = inputSessionId,
                //    DataContent = JsonUtil.object2JsonString(new VoidData()),
                //    Ack = true,
                //    AckId = CommonUtil.createUUID()
                //};
                //if (client.send(p))
                //{
                //    Console.WriteLine("send success");
                //}
                //else
                //{
                //    Console.WriteLine("send fail");
                //}

                for (int i = 0; i < 100000; i++)
                {
                    Chat c = new Chat() { Content = inputContent+i };
                    Packet p2 = new Packet()
                    {
                        Type = PacketType.EVENT_MESSAGE,
                        SubType = "chat",
                        SessionId = "3e9f5a38-1ae4-4142-9ff3-9668bff987ff",
                        DataContent = JsonUtil.object2JsonString(c),
                        Ack = true,
                        AckId = CommonUtil.createUniqueId()
                    };
                    client.send(p2);
                    Thread.Sleep(10);
                     
                }
            }

            Console.ReadKey();
        }



    }
}
